package com.next

class FlexFieldCategory
{
    String name
    SortedSet fields

    String objectType

    Component condition

    User createBy
    User modifyBy

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static hasMany = [fields: FlexField]
    static constraints = {
        name maxSize: 64, unique: true
        objectType inList: ['Account', 'Contact', 'Activity', 'Asset', 'Campaign', 'Contract', 'Issue', 'Lead', 'Opportunity']
        condition nullable: true, blank: true

        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
    }

    static mapping = {
        sort 'objectType', 'name'
        fields(lazy: false)
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    String toString()
    {
        name
    }
}
